var oTable;
var ReportModule = function() {
	var editable = function() {

		return {
			init : function() {
				
				oTable = $('#modelEditable')
						.dataTable(
								{
									"autoWidth":false,
				                    "lengthMenu": [
				                        [10, 15, 20, 50, -1],
				                        [10, 15, 20, 50, "All"]
				                    ],
				                    "displayLength": -1,
									"language" : {
										"lengthMenu" : "每页 _MENU_ 条记录",
										"paginate" : {
											"previous" : "上一页",
											"next" : "下一页"
										},
										"info" : "",//"从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
				                        "infoEmpty" : "",//记录数为0",
				                        "sZeroRecords": "找不到相关数据"
									},
									"bPaginate": false,  //是否显示分页
					                "bLengthChange": false,  //每页显示的记录数
									"columnDefs" : [ {
										"data" : null,
										"sortable" : false,
										"defaultContent" : "",
										"targets" : [ 2 ]
									} ],
									"columns" : [ {
										"data" : "fileName",
									}, {
										"data" : "uploadTime"
									}, {
										"data" : null
									} ],
									"filter" : false,
									"processing" : false,
									"serverSide" : true,
									"ajaxSource" : ctx
											+ "/investReport/query",
									"serverMethod" : "post",
									"fnRowCallback" : function(nRow, aData,
											displayIndex) {
										nRow.children[0].id = aData.id;
										$('td:eq(1)', nRow).html((new Date(aData.uploadTime)).Format("yyyy-MM-dd"));
										
										var opera = '<a class="download fa fa-download fa-lg" title="下载" href="javascript:window.location.href=\'investReport/downloadFile?id='+aData.id+'\';"></a>&nbsp;&nbsp;'+
		                	   			   '<a class="delete fa fa-trash-o fa-lg" title="删除" href="javascript:;"></a>';
									    $('td:eq(2)', nRow).html(opera);

										return nRow;
									},
									"fnServerData" : function(ajaxSource,dataSet, fnCallback) {
											$.ajax({
													"contentType" : "application/json; charset=UTF-8",
													"dataType" : 'json',
													"type" : "POST",
													"url" : ajaxSource,
													"data" : '{"productId":"'+$("#productId").val()+'"}',
													"success" : fnCallback
												});
									       }
								});
			    // 删除
                $('#modelEditable').on("click", "a.delete", function (e) {
                    e.preventDefault();
                    
                    var obj = $(this);
                    bootbox.confirm("您确定删除这条记录?", function(result) {
                        if (result) {
                        	var nRow = obj.parents('tr')[0];
                            var id = nRow.children[0].id;
                            $.ajax({
                            	type:"post",
                                url: ctx + "/investReport/delete",
                                data:"id=" + id,
                                dataType:"json",
                                success: function(msg){
                                	if (msg.code == 1) {
                                		bootbox.alert("删除成功！");
                                		oTable.fnDeleteRow(nRow);
                                	} else {
                                		bootbox.alert("删除失败！");
                                	}
                                },
                                error: function (msg) {
                                	bootbox.alert(msg.message);
                                }
                           
                            });
                        }
                    });
                });
                // 编辑
//                $('#modelEditable').on("click", "a.edit", (function (e) {
//                    e.preventDefault();
//                    var nRow = $(this).parents('tr')[0];
//                    var id = nRow.children[0].id;
//                    $("#netId").val(id);
//                    $("#old_unitNet").val(nRow.children[0].name);
//                    $("#new_unitNet").val(nRow.children[0].name);
//                    _showDialog();
//                    
//                }));
			}
		};

	}();

	return {
		init : function() {
			editable.init();
		}
	};
}();

var ProductReportAdd = function() {
	var handleValidation = function() {
		var formAdd = $('#addForm');

		var validator = formAdd.validate({
//			errorElement : 'span', // default input error message container
//			errorClass : 'help-block', // default input error message class
//			focusInvalid : false, // do not focus the last invalid input
//			ignore : "",
			rules : {
					report : {
						required : true,
						file : true
					}
			},
			messages: {
				report: {required:"投后报告不能为空！"}
            },
			errorPlacement : function(error, element) { // render error
				// placement for each
				// input type
				var icon = $(element).parent('.input-icon').children('i');
				icon.removeClass('fa-check').addClass("fa-warning");
				icon.attr("data-original-title", error.text()).tooltip({
					'container' : 'body'
				});
			},

			highlight : function(element) { // hightlight error inputs
				$(element).closest('.form-group').addClass('has-error'); // set
				// error
				// class
				// to
				// the
				// control
				// group
			},

			unhighlight : function(element) { // revert the change done by
				// hightlight

			},

			success : function(label, element) {
				var icon = $(element).parent('.input-icon').children('i');
				$(element).closest('.form-group').removeClass('has-error')
						.addClass('has-success'); 
				icon.removeClass("fa-warning").addClass("fa-check");
			},
			submitHandler : function(form) {
			}
		});
	}

	return {
		init : function() {
			handleValidation();
		}
	};
}();

function toPercent(num) {
	return Number(num * 100).toFixed(2) + '%';
}